import 'package:flutter/material.dart';
import 'package:zhrs/mainpage/mainindex.dart';
import 'package:zhrs/mainpage/webviewpage.dart';
import '../model/user_model.dart';
import '../utils/alert_utils.dart';
import '../utils/sp_utils.dart';
import '../utils/alert_utils.dart';
import '../pages/index.dart';
import '../pages/login.dart';
import 'app_context.dart';

final CONSTANT_USERS=[
  UserModel(userId: "1",loginname: "111111",username:"成年男",password: "111111"),
  UserModel(userId: "2",loginname: "222222",username:"成年女",password: "222222"),
  UserModel(userId: "3",loginname: "333333",username:"老年人",password: "333333"),
];

class HttpManager {
  static login(BuildContext context, UserModel userModel,bool isToIndex,)  async {
    UserModel loginUser;
    //模拟登陆
    CONSTANT_USERS.forEach((user){
      if(user.loginname==userModel.loginname&&user.password==userModel.password){
        loginUser=user;
        loginUser.loginTime= DateTime.now().toIso8601String();
      }
    });
    if(loginUser==null){
      alertDialog(context, "用户名或密码不正确！");
      return;
    }
    APP.userModel=loginUser;
    //模拟登陆成功
    SpUtils spUtils = SpUtils();

    await spUtils.init().then((sp){
      sp.setString("USER", loginUser.toJsonString());
      if(isToIndex)

           //Navigator.push(context,MaterialPageRoute(builder:(context) =>IndexPage()));
       Navigator.push(context,MaterialPageRoute(builder:(context) =>Mainindex( index:int.parse(loginUser.userId) )));
     // Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder:(context) =>Mainindex(index:int.parse(APP.userModel.userId,))),(route) => route == null);


    });

  }
//登出
  static logout(BuildContext context) async{
    //UserContext.clear();
    //模拟登出
    APP.userModel=null;
    SpUtils spUtils = SpUtils();
    await spUtils.init().then((sp){
      sp.setString("USER", "");
      //Navigator.push(context,MaterialPageRoute(builder:(context) =>LoginPage()));

    //  bool Function(Route<dynamic> route);
   // Webviewpage.pages.controller.evaluateJavascript("localStorage.clear()");
    //  Webviewpage s=new Webviewpage();
    //  s.webviewpages.controller..evaluateJavascript("localStorage.clear()");
    //  Webviewpage.pages.controller.evaluateJavascript(
    //      'startRecordBackInfo("sss")')
     //     ?.then((result) {
        //print("result:$result");
     // });
      Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder:(context) =>LoginPage()),(ss){
                   return false;
      });
    });
  }
}
